package com.shanghaionstar.carmanage;

import android.Manifest;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;

import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.framework.mvp.base.core.PermissionCall;
import com.onstar.cn.map.OMapView;
import com.onstar.cn.map.common.LocationType;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.nav.NavAroundDealerFragment;
import com.shanghaionstar.nav.NavMainFragment;
import com.shanghaionstar.nav.NavMapFragment;
import com.shanghaionstar.nav.model.LocateStatus;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.utils.MapHandler;
import com.shanghaionstar.utils.NavHolder;

/**
 * Created by 6335yz on 2016/8/4.
 */
@ContentView(R.layout.car_manage_around_dealer)
public class AroundDealerActivity extends AbstractMapActivity implements MapHandler.OnMapCallBack {
    @FindViewById
    private ProgressLayout package_list_progress;
    @FindViewById
    private OMapView mapview;
    private NavMapFragment navMapFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        package_list_progress.setVisibility(View.VISIBLE);
        mapHandler = new MapHandler(this, mapview.getMap());
        mapHandler.registOnMapCallBack(this);
//        BusinessUtil.sendReportByClickType(this, FunctionNameFindId.POIviewDealerSearch.getStringValue());
        requestRunTimePermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, new PermissionCall() {
            @Override
            public void requestSuccess() {
                requestMyLocate();
                NavHolder.getInstance(AroundDealerActivity.this).findMyLocation();
            }

            @Override
            public void refused() {

            }
        });

    }

    /**
     * 主动请求位置变化信息
     */
    public void requestMyLocate() {
        NavHolder navHolder = NavHolder.getInstance(this);
        /**
         * 一直报 enum的ordinal（）方法错误
         */
        LocateStatus locateStatus = navHolder.getLocateStatus();
        switch (locateStatus) {
            case LOCATING:
                break;
            case LOCATED:
                if (navHolder.getMyLocation() != null) {
                    mapHandler.geoCoderGps(navHolder.getMyLocation().getLatitude(), navHolder.getMyLocation().getLongitude(), LocationType.MYSELF);
                }
                break;
            case LOCATE_FAIL:
                package_list_progress.setVisibility(View.GONE);
                finish();
                break;
        }
    }

    @Override
    public void onFindLocateSuccess(LocationType locationType) {
        package_list_progress.setVisibility(View.GONE);
        mapHandler.setMapPoi_PoiItemInfo(mapHandler.getMyLocationPoiItemInfo());
        getNavDataHolder().setCurrentMapPoi(mapHandler.getMyLocationPoiItemInfo());
//        Intent intent = new Intent(this, NavActivity.class);
//        Bundle bundle = new Bundle();
//        bundle.putString(FinalConstants.JUMP_FLAG, NavMapFragment.class.getSimpleName());
//        bundle.putSerializable("poiItemInfo",mapHandler.getMyLocationPoiItemInfo());
//        intent.putExtras(bundle);
//        startActivity(intent);
        addFragment(getSupportFragmentManager(), new NavMapFragment(), true);
        addFragment(getSupportFragmentManager(), NavAroundDealerFragment.newInstance(mapHandler.getMyLocationPoiItemInfo()), true);
        showFragment(getSupportFragmentManager(), NavAroundDealerFragment.class, NavMapFragment.class);
    }

    @Override
    public void onFindLocateFail() {

    }

    @Override
    public void initMapHandler(MapHandler mapHandler) {

    }

    @Override
    public void showPoiToMap(PoiItemInfo poiItemInfo, LocationType locationType) {
        if (mapHandler != null) {
            switch (locationType) {
                case POI:
                case VEHICLE:
                case MYSELF:
                    /**
                     * 共有三种poi信息，其他两种记录-- 我的位置，和车辆位置,在Maphandler中得到poi后赋值
                     */
                    if (locationType == LocationType.POI) {
                        getNavDataHolder().setSearchPoi(poiItemInfo);
                    }
                    mapHandler.showLocation(locationType, poiItemInfo);
                    mapHandler.setMapPoi_PoiItemInfo(poiItemInfo);
                    getNavDataHolder().setCurrentMapPoi(poiItemInfo);
                    removeUntilFragment(getSupportFragmentManager(), NavMainFragment.class, false);
                    showFragment(getSupportFragmentManager(), NavMapFragment.class, getClass());
                    showBottomView(poiItemInfo, true);
                    break;
                case FOOTPRINT:
                    mapHandler.showLocation(locationType, poiItemInfo);
                    break;
            }
        }
    }

    /**
     * 用于从其他页面跳转到map页面；
     * 地图栏，用来显示导航，我的详情等快捷信息
     *
     * @param isShow
     */
    public void showBottomView(PoiItemInfo poiItemInfo, boolean isShow) {
        navMapFragment = (NavMapFragment) getSupportFragmentManager().findFragmentByTag(NavMapFragment.class.getSimpleName());
        if (navMapFragment != null) {
            navMapFragment.showBottomView(poiItemInfo, isShow);
        }
    }


    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void onBackPressed() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        log.d("onBackPressed current fragment tag is----->>" + getCurrentFragmentTag());
        String currentTag = getCurrentFragmentTag();
        BaseFragment current = (BaseFragment) (fragmentManager.findFragmentByTag(currentTag));
        if (current != null) {
            if (fragmentManager.getBackStackEntryCount() == 1) {
                this.finish();
            } else {
                current.onFragmentBackPress();
            }
        }
    }

    /**
     * 地图点的图标
     *
     * @return
     */
    @Override
    public int getmMyLocationArrow() {
        return R.drawable.nav_map_marker_my;
    }

    /**
     * Vehicle location
     */
    @Override
    public int getmVehicleLocationArrow() {
        return R.drawable.nav_map_marker_vehicle;
    }

    /**
     * POI location
     */
    @Override
    public int getmPoiLocationArrow() {
        return R.drawable.nav_map_marker_poi;
    }

    @Override
    public int getmFootPrintArror() {
        return R.drawable.nav_foot_print_marker;
//        return R.drawable.onstar_geofence_preview_icon;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapHandler.removeOnMapCallBack();
    }
}
